package com.cmwa.solution.business.infodisclosure.institution.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.infodisclosure.institution.dao.InstRolRelationsDao;
import com.cmwa.solution.business.infodisclosure.institution.dao.InstitutionDao;
import com.cmwa.solution.business.infodisclosure.institution.entity.InstRolRelationsVo;
import com.cmwa.solution.business.infodisclosure.institution.entity.InstitutionVo;
import com.cmwa.solution.business.infodisclosure.institution.service.InstitutionService;
import com.cmwa.solution.utils.SessionUtils;

@Service
public class InstitutionServiceImpl implements InstitutionService {

	@Resource
	private InstitutionDao institutionDao;
	@Resource
	private InstRolRelationsDao instRolRelationsDao;
	
	@Override
	public List<InstitutionVo> getInstitutionList(InstitutionVo institutionVo) {
		return institutionDao.getInstitutionList(institutionVo);
	}
	
	@Override
	public void insertInstitutionInfo(InstitutionVo institutionInfo) {
		String id = SessionUtils.getEmployee().getID();
		institutionInfo.setCreateId(id);
		institutionDao.insertInstitutionInfo(institutionInfo);
		
		if(StringUtils.isNotBlank(institutionInfo.getInstRolType())) {
			List<InstRolRelationsVo> instRolRelationsList = new ArrayList<>();
			String[] types = institutionInfo.getInstRolType().split(",");
			for (String type : types) {
				InstRolRelationsVo InstRolRelationsVo = new InstRolRelationsVo();
				InstRolRelationsVo.setInstId(institutionInfo.getInstId());
				InstRolRelationsVo.setCreateId(id);
				InstRolRelationsVo.setInstRolType(type);
				instRolRelationsList.add(InstRolRelationsVo);
			}
			if (CollectionUtils.isNotEmpty(instRolRelationsList)) {
				instRolRelationsDao.batchInsertInstRolRelations(instRolRelationsList);
			}
		}
	}
	
		
		
	@Override
	public void physicDeleteInstitutionInfoById(String id) {
		institutionDao.physicDeleteInstitutionInfoById(id);
	}
	
		
		
	@Override
	public void logicDeleteInstitutionInfoById(String id) {
		institutionDao.logicDeleteInstitutionInfoById(id);
	}
	
		
		
	@Override
	public void updateInstitutionInfoById(InstitutionVo institutionInfo) {
		institutionDao.updateInstitutionInfoById(institutionInfo);
	}
	
		
		
	@Override
	public Page<InstitutionVo> institutionInfoListPage(SearchParam sp) {
		List<InstitutionVo> items = institutionDao.institutionInfoListPage(sp);
		return Page.create(items, sp.getStart(), sp.getLimit(), sp.getTotal());
	}
	
		
		
	@Override
	public InstitutionVo getInstitutionInfoById(String id) {
		return institutionDao.getInstitutionInfoById(id);
	}

	@Override
	public List<InstitutionVo> getInstListLimit(String name, Integer limit) {
		return institutionDao.getInstListLimit(name, limit);
	}
	
	@Override
	public boolean checkRepeat(String instId, String instCode, String instSName,
			String instLName) {
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("instId", instId);
		param.put("instSName", instSName);
		param.put("instLName", instLName);
		int count = institutionDao.getCountByParam(param);
		return count > 0;
	}
}
